package cn.texous.util.commons.util.encrypt;

import java.math.BigInteger;

/**
 * insert description here
 *
 * @author Showa.L
 * @since 2019/8/15 13:43
 */
public class HexStringUtils {

    public static byte[] getByte(String hexStr) {
        return new BigInteger(hexStr, 16).toByteArray();
    }

    /***/
    public static byte[] getByteUnSign(String hexStr) {
        int len = hexStr.length() / 2;
        byte[] result = new byte[len];
        char[] achar = hexStr.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (charToByte(achar[pos]) << 4 | charToByte(achar[pos + 1]));
        }
        return result;
    }

    /***/
    public static int[] convertFromHexStringToInts(String s) {
        int len = (int) Math.ceil(s.length() / 8.0);
        int[] ints = new int[len];
        for (int i = 0; i < len; i++) {
            String part = s.substring(8 * i, 8 * i + 8);
            ints[i] = (new BigInteger(part, 16)).intValue();
        }
        return ints;
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789abcdef".indexOf(c);
    }

}
